package com.zhuangfei.hpuhelper.view;

import java.util.ArrayList;
import java.util.List;

import com.bigkoo.pickerview.OptionsPickerView;
import com.hpu.bean.Course;
import com.hpu.constants.ShareConstants;
import com.hpu.parser.CourseParser;
import com.hpu.tools.InputMethodTools;
import com.hpu.tools.ShareTools;
import com.zhuangfei.hpuhelper.tools.ActivityTools;
import com.zhuangfei.hpuhelper.tools.CourseTools;
import com.zhuangfei.smartalert.core.ChooseViewAlert;
import com.zhuangfei.smartalert.core.DayAlert;
import com.zhuangfei.smartalert.core.WeekAlert;
import com.zhuangfei.smartalert.listener.OnChooseViewAdapter;
import com.zhuangfei.smartalert.listener.OnDayAlertListener;
import com.zhuangfei.smartalert.listener.OnWeekAlertListener;
import com.zhuangfei.toolkit.model.BundleModel;
import com.zhuangfei.toolkit.tools.BundleTools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.lzf.serviceplatform.R;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class AddCourseActivity extends Activity implements OnWeekAlertListener{

	@BindView(R.id.id_addcourse_name)
	public EditText nameEditText;

	@BindView(R.id.id_addcourse_room)
	public EditText roomEditText;

	@BindView(R.id.id_addcourse_teacher)
	public EditText teacherEditText;

	@BindView(R.id.id_addcourse_weeks_textview)
	public TextView weekTextView;

	@BindView(R.id.id_addcourse_days_textview)
	public TextView dayTextView;

	ArrayList<String> optionsItems;
	ArrayList<ArrayList<String>> options2Items;
	ArrayList<ArrayList<ArrayList<String>>> options3Items;

	private WeekAlert weekAlert;

	private List<Integer> weeksList=new ArrayList<>();
	private int day=-1;
	private int start=-1;
	private int step=-1;

	Class returnClass;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_course);
		ActivityTools.setTransparent(this);
		ButterKnife.bind(this);
		ShareTools.put(this, "course_update", 1);
		initsData();
	}

	private void initsData() {
		returnClass=BundleTools.getFromClass(this,CourseManagerActivity.class);
		String name=BundleTools.getString(this,"name","");
		String teacher=BundleTools.getString(this,"teacher","");

		nameEditText.setText(name);
		teacherEditText.setText(teacher);

		weekAlert=new WeekAlert(this);
		weekAlert.setTitle("选择周数")
				.setCancelEnable(true)
				.setOnWeekAlertListener(this)
				.create();

		String[] dayArray = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
		String[] startArray = {"第一节", "第二节", "第三节", "第四节", "第五节", "第六节", "第七节", "第八节", "第九节", "第十节", "第十一节", "第十二节"};
		optionsItems = new ArrayList<>();
		options2Items = new ArrayList<>();
		options3Items = new ArrayList<>();
		for (int i = 0; i < dayArray.length; i++) {
			optionsItems.add(dayArray[i]);
			ArrayList<String> optionItems2_01 = new ArrayList<>();
			ArrayList<ArrayList<String>> optionItems3_01 = new ArrayList<>();

			for (int j = 0; j < startArray.length; j++) {
				optionItems2_01.add(startArray[j]);
				ArrayList<String> optionItems3_01_01 = new ArrayList<>();
				for (int m = j; m < startArray.length; m++) {
					optionItems3_01_01.add(startArray[m]);
				}
				optionItems3_01.add(optionItems3_01_01);
			}
			options2Items.add(optionItems2_01);
			options3Items.add(optionItems3_01);
		}
	}

	@OnClick(R.id.id_addcourse_days)
	public void showOptionDialog() {
		InputMethodTools.close(this);
		//条件选择器
		OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
			@Override
			public void onOptionsSelect(int options1, int option2, int options3, View v) {
				//返回的分别是三个级别的选中位置
				String tx = optionsItems.get(options1)
						+ " "+options2Items.get(options1).get(option2)
						+ "至"+options3Items.get(options1).get(option2).get(options3);
				dayTextView.setText(tx);
				day=options1+1;
				start=option2+1;
				step=options3+1;
			}
		}).setSubmitText("确定")//确定按钮文字
				.setCancelText("取消")//取消按钮文字
				.setTitleText("节次选择")//标题
				.build();

		pvOptions.setPicker(optionsItems, options2Items, options3Items);
		pvOptions.show();
	}

	@OnClick(R.id.id_back)
	public void goBack(){
		ActivityTools.toBackActivityAnim(this,returnClass);
	}

	@Override
	public void onBackPressed() {
		goBack();
	}

	@OnClick(R.id.id_addcourse_savebutton)
	public void save(){
		String name=nameEditText.getText().toString().trim();
		String teacher=teacherEditText.getText().toString().trim();
		String room=roomEditText.getText().toString().trim();
		String term=(String) ShareTools.get(this,ShareConstants.STORE_TERM,"default_term");
		if(start==-1||step<0||name==null||room==null||name.length()==0||room.length()==0||room.length()==0||day==-1){
			Toast.makeText(this, "不可为空!",Toast.LENGTH_SHORT).show();
			return;
		}
		Course course=new Course(term,name, room, teacher, weeksList, start, step, day, -1,"");
		boolean isSuccess=CourseTools.saveCourse(this,course);

		if(isSuccess){
			Toast.makeText(this, "保存成功",Toast.LENGTH_SHORT).show();
			ActivityTools.toBackActivityAnim(this,returnClass);
		}else{
			Toast.makeText(this, "课程重复!",Toast.LENGTH_SHORT).show();
		}
	}

	@OnClick(R.id.id_addcourse_weeks)
	public void showWeekAlert() {
		InputMethodTools.close(this);
		weekAlert.setDefault(weeksList)
				.show();
	}

	@Override
	public void onConfirm(WeekAlert messageAlert, List<Integer> result) {
		String res="";
		for(int i=0;i<result.size();i++){
			res+=result.get(i);
			if(i!=result.size()-1) res+=",";
		}
		messageAlert.hide();
		weeksList=result;
		if(result.size()==0) weekTextView.setHint("点击选择周数");
		else{
			weekTextView.setText(res+"周上");
		}
	}

	@Override
	public void onCancel(WeekAlert messageAlert) {
		messageAlert.hide();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(weekAlert!=null){
			weekAlert.dimiss();
		}
	}
}
